package net.pm.common.toolkit.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
   *   日期工具类
 *
 */
public abstract class DateUtils {

	/**英文简写(默认)如:2012-03-26
	 */
	public static String FORMAT_SHORT = "yyyy-MM-dd";
	
	/**英文全称
	 */
	public static String FORMAT_LONG = "yyyy-MM-dd HH:mm:ss";
	
	/**精确到毫秒的完整时间
	 */
	public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S";
	
	/**中文简写
	 */
	public static String FORMAT_SHORT_CN = "yyyy年MM月dd日";
	
	/**中文全称
	 */
	public static String FORMAT_LONG_CN = "yyyy年MM月dd日 HH时mm分ss秒";
	
	/**精确到毫秒的完整中文时间
	 */
	public static String FORMAT_FULL_CN = "yyyy年MM月dd日 HH时mm分ss秒S毫秒";
	
	public static SimpleDateFormat shortDateFormat = new SimpleDateFormat(DateUtils.FORMAT_SHORT);
	public static SimpleDateFormat longDateFormat = new SimpleDateFormat(DateUtils.FORMAT_LONG);
	public static SimpleDateFormat fullDateFormat = new SimpleDateFormat(DateUtils.FORMAT_FULL);;
	public static SimpleDateFormat shortDateFormatCN = new SimpleDateFormat(DateUtils.FORMAT_SHORT_CN);;
	public static SimpleDateFormat longDateFormatCN = new SimpleDateFormat(DateUtils.FORMAT_LONG_CN);;
	public static SimpleDateFormat fullDateFormatCN = new SimpleDateFormat(DateUtils.FORMAT_FULL_CN);
	
	/**根据预设格式返回当前日期
	 * @return
	 */
	public static String obtainCurrentStrdate(){
		return format(new Date());
	}
	
	/**根据用户格式返回当前日期
	 * @param datepattern
	 * @return
	 */
	public static String obtainCurrentStrdate(String datepattern){
		return format(new Date(),datepattern);
	}
	/**根据预设格式返回当前日期
	 * @return
	 */
	public static Date obtainCurrentDate(){
		return parse(obtainCurrentStrdate());
	}
	
	/**根据用户格式返回当前日期
	 * @param datepattern
	 * @return
	 */
	public static Date obtainCurrentDate(String datepattern){
		return parse(obtainCurrentStrdate(datepattern),datepattern);
	}
	
	/**获取日期后x个月的日期
	 * @param date   日期
	 * @param x　　　　要增加的月数
	 * @return
	 */
	public static Date obtainAfterMonthX(Date date,int x){
		Calendar dar = Calendar.getInstance();
		dar.setTime(date);
		dar.add(Calendar.MONTH, x);
		return dar.getTime();
	}
	
	/**获取日期后x天的日期
	 * @param date
	 * @param x
	 * @return
	 */
	public static Date obtainAfterDayX(Date date,int x){
		Calendar dar = Calendar.getInstance();
		dar.setTime(date);
		dar.add(Calendar.DATE, x);
		return dar.getTime();
	}
	
	/**获取日期前x个月的日期
	 * @param date   日期
	 * @param x　　　　要增加的月数
	 * @return
	 */
	public static Date obtainBeforeMonthX(Date date,int x){
		Calendar dar = Calendar.getInstance();
		dar.setTime(date);
		dar.add(Calendar.MONTH, -x);
		return dar.getTime();
	}
	
	/**获取日期前x天的日期
	 * @param date
	 * @param x
	 * @return
	 */
	public static Date obtainBeforeDayX(Date date,int x){
		Calendar dar = Calendar.getInstance();
		dar.setTime(date);
		dar.add(Calendar.DATE, -x);
		return dar.getTime();
	}
	
	/**获取时间戳
	 * @return
	 */
	public static String obtainTimeStr(){
		Calendar dar = Calendar.getInstance();
		return fullDateFormat.format(dar.getTime());
	}
	
	/**获取日期距离今天的天数
	 * @param date　日期
	 * @return
	 */
	public static int obtainDistanceNow(Date date){
		Calendar dar = Calendar.getInstance();
		long nowmillis = dar.getTimeInMillis();
		dar.setTime(date);
		long refmillis = dar.getTimeInMillis();
		return (int)(nowmillis - refmillis)/(1000*3600*24);
	};
	
	/**获取两个日期相差的天数
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int obtainDistanceDoubleDay(Date date1, Date date2) {
		Calendar dar1 = Calendar.getInstance();
		Calendar dar2 = Calendar.getInstance();
		dar1.setTime(date1);
		dar2.setTime(date2);
		
		long millis1 = dar1.getTimeInMillis();
		long millis2 = dar2.getTimeInMillis();
		
		return (int)((millis1 - millis2) / (1000 * 3600 * 24));
	}
	/**获取年份
	 * @param date
	 * @return
	 */
	public static String obtainYear(Date date){
		return format(date).substring(0,4);
	}
	
	/**获得默认的 date pattern
	 * @return
	 */
	public static String obtainDefaultDatePattern(){
		return FORMAT_LONG;
	}
	
	/**使用用户格式格式化日期
	 * @param date      日期
	 * @param pattern	日期格式
	 * @return
	 */
	public static String format(Date date,String pattern){
		String retval = "";
		if(date != null){
			SimpleDateFormat df = chooseDateFormatBean(pattern);
			retval = df.format(date);
		}
		return retval;
	}
	
	/**使用预设格式格式化日期
	 * @param date
	 * @return
	 */
	public static String format(Date date){
		return format(date, obtainDefaultDatePattern());
	}
	
	/**使用用户格式提取字符串日期
	 * @param strDate
	 * @param pattern
	 * @return
	 */
	public static Date parse(String strDate,String pattern){
		SimpleDateFormat df = chooseDateFormatBean(pattern);
		try {
			return df.parse(strDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**使用预设格式提取字符串日期
	 * @param strDate
	 * @return
	 */
	public static Date parse(String strDate){
		return parse(strDate,obtainDefaultDatePattern());
	}
	
	
	/**选择资源
	 * @param pattern
	 * @return
	 */
	private static SimpleDateFormat chooseDateFormatBean(String pattern){
		SimpleDateFormat chooseDateFormat = null;
		if(pattern == DateUtils.FORMAT_SHORT)
			chooseDateFormat = DateUtils.shortDateFormat;
		else if(pattern == DateUtils.FORMAT_LONG)
			chooseDateFormat = DateUtils.longDateFormat;
		else if(pattern == DateUtils.FORMAT_FULL)
			chooseDateFormat = DateUtils.fullDateFormat;
		else if(pattern == DateUtils.FORMAT_SHORT_CN)
			chooseDateFormat = DateUtils.shortDateFormatCN;
		else if(pattern == DateUtils.FORMAT_LONG_CN)
			chooseDateFormat = DateUtils.longDateFormatCN;
		else if(pattern == DateUtils.FORMAT_FULL_CN)
			chooseDateFormat = DateUtils.fullDateFormatCN;
		else{
			System.out.println("pattern: "+pattern+" ,没有该pattern ,发生于-"+DateUtils.class.getName());
			return null;
		}
		return chooseDateFormat;
	}
	
}
